package com.belf.web.controller.console;

import com.belf.common.annotation.Log;
import com.belf.common.core.controller.BaseController;
import com.belf.common.core.domain.AjaxResult;
import com.belf.common.core.domain.entity.SysUser;
import com.belf.common.core.page.TableDataInfo;
import com.belf.common.enums.BusinessType;
import com.belf.common.utils.SecurityUtils;
import com.belf.common.utils.poi.ExcelUtil;
import com.belf.console.domain.DataCheckTask;
import com.belf.console.service.IDataCheckTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 校验任务Controller
 * 
 * @author belf
 * @date 2024-07-16
 */
@RestController
@RequestMapping("/console/taskCheck")
public class DataCheckTaskController extends BaseController
{
    @Autowired
    private IDataCheckTaskService dataCheckTaskService;

    /**
     * 查询校验任务列表
     */
    @PreAuthorize("@ss.hasPermi('console:taskCheck:list')")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody DataCheckTask dataCheckTask)
    {
        // 管理员拥有所有权限
        if (!SysUser.isAdmin(SecurityUtils.getUserId())){
            dataCheckTask.setCreateBy(getUsername());
        }
        List<DataCheckTask> list = dataCheckTaskService.selectDataCheckTaskList(dataCheckTask);
        long count = dataCheckTaskService.selectDataCount(dataCheckTask);
        return getDataTablePost(list,count);
    }

    /**
     * 导出校验任务列表
     */
    @PreAuthorize("@ss.hasPermi('console:taskCheck:export')")
    @Log(title = "校验任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataCheckTask dataCheckTask)
    {
        List<DataCheckTask> list = dataCheckTaskService.selectDataCheckTaskList(dataCheckTask);
        ExcelUtil<DataCheckTask> util = new ExcelUtil<DataCheckTask>(DataCheckTask.class);
        util.exportExcel(response, list, "校验任务数据");
    }

    /**
     * 获取校验任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('console:taskCheck:query')")
    @PostMapping("/getInfo")
    @ResponseBody
    public AjaxResult getInfo(@RequestBody DataCheckTask dataCheckTask)
    {
        return success(dataCheckTaskService.selectDataCheckTaskById(dataCheckTask.getId()));
    }

    /**
     * 新增校验任务
     */
    @PreAuthorize("@ss.hasPermi('console:taskCheck:add')")
    @Log(title = "校验任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody DataCheckTask dataCheckTask)
    {
        return toAjax(dataCheckTaskService.insertDataCheckTask(dataCheckTask));
    }

    /**
     * 修改校验任务
     */
    @PreAuthorize("@ss.hasPermi('console:taskCheck:edit')")
    @Log(title = "校验任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody DataCheckTask dataCheckTask)
    {
        return toAjax(dataCheckTaskService.updateDataCheckTask(dataCheckTask));
    }

    /**
     * 删除校验任务
     */
    @PreAuthorize("@ss.hasPermi('console:taskCheck:remove')")
    @Log(title = "校验任务", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody DataCheckTask dataCheckTask)
    {
        return toAjax(dataCheckTaskService.deleteDataCheckTaskByIds(dataCheckTask.getIdArray()));
    }

    /**
     * 执行评估任务
     */
    @PreAuthorize("@ss.hasPermi('console:taskCheck:edit')")
    @Log(title = "校验任务", businessType = BusinessType.UPDATE)
    @PostMapping("/execute")
    public AjaxResult execute(@RequestBody DataCheckTask dataCheckTask)
    {
        return dataCheckTaskService.executeTaskById(dataCheckTask.getId());
    }

    /**
     * 启动调度器
     */
    @PreAuthorize("@ss.hasPermi('console:taskCheck:edit')")
    @Log(title = "校验任务", businessType = BusinessType.UPDATE)
    @PostMapping("/scheduler")
    public AjaxResult scheduler(@RequestBody DataCheckTask dataCheckTask)
    {
        return dataCheckTaskService.schedulerTask(dataCheckTask.getIdArray());
    }
}
